package product;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Hollyday extends JFrame {

	DefaultTableModel model = new DefaultTableModel();
	Vector<String> colName = new Vector<String>();
	Vector<Integer> vc = new Vector<Integer>();
	Admin adm;
	Employee epl;
	private JPanel contentPane;
	JScrollPane scrollPane;
	JTable table;


	public Hollyday(Object obj){
		
		if(obj instanceof Admin) this.adm=(Admin)obj;
		else if(obj instanceof Employee) this.epl = (Employee)obj;
		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(Hollyday.this);
		}
		catch(Exception ex){}

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if(adm!=null) adm.hdFlag=false;
				else if(epl!=null) epl.hdFlag=false;
				model.setRowCount(0);
			}
			
		});
		
		setTitle("휴가 상세 정보");
		if(epl!=null){
			colName.add("이름");
		}
		if(adm!=null){
			colName.add("사번");
			colName.add("이름");
		}
		colName.add("월차");
		colName.add("년차");
		colName.add("생리");
		colName.add("기타");
		colName.add("총휴가일");
		colName.add("년/월");
		setBounds(600,300, 600, 150);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		
		JTableHeader jh = table.getTableHeader();
		jh.setReorderingAllowed(false);//컬럼 이동 금지		
	}


	public void setTable(String id, String name, String ym, SalarySQL db){
		
		//if(model!=null) model.setRowCount(0);
		String[] str = ym.split("/");
		String date="";
		if(ym.equals("")) date="전체 년/월";
		else{
			if(str[0].equals("%")) date = "전체 년 ";
			else date = "20"+str[0]+"년 ";
			
			if(str.length>1){
				str[1]=str[1]+"월 ";
				date += str[1];
			}
		}
		db.holyTable(id, name, ym, model,date);
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			model = new DefaultTableModel(colName, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table = new JTable(model);
			table.setAutoCreateRowSorter(true);
		}
		return table;
	}

}
